package com.qlm.tool;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;


public class WxUtil {
	private static String charset = "UTF-8";
    /**
     * 日期格式
     */
    public static String PATTEN_YMD = "yyyy-MM-dd";
    /**
     * 获取当前日期字符串
     * 
     * @param formart 日期格式
     * @return 当前日期字符串
     */
    public static String getDateFormatStr(String formart) {
        return new SimpleDateFormat(formart).format(new java.util.Date());
    }
    
	public static String getOrderNo(String partner) {
		String order = partner
				+ new SimpleDateFormat("yyyyMMdd").format(new Date());
		Random r = new Random();
		for (int i = 0; i < 10; i++) {
			order += r.nextInt(9);
		}
		return order;
	}
    /**
     * 获取当前时间，字符串类型，格式（yyyy-mm-dd）
     * 
     * @return String
     */
    public static String getCurrentDateString() {
        return getDateFormatStr(PATTEN_YMD);
    }
	/**
	 * 随机16为数值
	 * 
	 * @return
	 */
	public static String buildRandom() {
		String currTime = getCurrentDateString();
		String strTime = currTime.substring(8, currTime.length());
		int num = 1;
		double random = Math.random();
		if (random < 0.1) {
			random = random + 0.1;
		}
		
		for (int i = 0; i < 4; i++) {
			num = num * 10;
		}
		return (int) ((random * num)) + strTime;
	}
	public static String createSign(Map<String, String> map,String partnerKey) {
		TreeMap<String, String> packageParams = new TreeMap<String, String>(map);
		// 去除参与的参数sign
		packageParams.remove("sign");
		// 遍历排序后的字典，将所有参数按"key=value"格式拼接在一起
				StringBuilder sb = new StringBuilder();
				boolean first = true;
		for (Map.Entry<String, String> param : packageParams.entrySet()) {
			String value = param.getValue();
			if (StrKit.isBlank(value)) {
				continue;
			}
			if (first) {
				first = false;
			} else {
				sb.append("&");
			}
			sb.append(param.getKey()).append("=");
//			if (true) {
//				try { value = URLEncoder.encode(value, charset).replace("+", "%20"); } catch (UnsupportedEncodingException e) {}
//			}
			sb.append(value);
		}
		String sign = MD5Util.MD5Encode(sb.toString()+"&key="+partnerKey, charset).toUpperCase();
		return sign;
	}
	
	 private static final String BASE_CHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

	    /**
	     * 或得随机字符串
	     * 
	     * @param length
	     * @return String
	     */
    public static String getRandomString(int length) {
        Random random = new Random();
        StringBuffer sbReturn = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(BASE_CHAR.length());
            sbReturn.append(BASE_CHAR.charAt(number));
        }
        return sbReturn.toString();
    }
	
	/**
	 * 获取整型  可以保证正确的获取数据库正确的整型
	 * @param record
	 * @return
	 */
	public static Integer getInt(String key , Record record){
		Object object = record.get(key);
		return getInt(object);
	}
	
	public static Integer getInt(String key , Record record,int defal){
		Object object = record.get(key);
		return getInt(object,defal);
	}
	
	public static Integer getInt(String key , Model<?> model,int defal){
		Object object = model.get(key);
		return getInt(object,defal);
	}
	
	/**
	 * 获取整型  可以保证正确的获取数据库正确的整型
	 * @param key
	 * @param model
	 * @return
	 */
	public static Integer getInt(String key ,Model<?> model){
		Object object = model.get(key);
		return getInt(object);
	}
	
	private static Integer getInt(Object obj){
		if(obj == null) return 0;
		Integer result = 0;
		if(obj instanceof Number){
			Number cast = Number.class.cast(obj);
			result = cast.intValue();
		}else{
			result = null;
		}
		return result;
	}
	
	private static Integer getInt(Object obj,int defaultVal){
		if(obj == null) return defaultVal;
		Integer result = 0;
		if(obj instanceof Number){
			Number cast = Number.class.cast(obj);
			result = cast.intValue();
		}else{
			result = defaultVal;
		}
		return result;
	}
	
	/**
	 * 获取String
	 * @param key
	 * @param model
	 * @return
	 */
	public  static  String getStr(String key,Model<?> model){
		Object object = model.get(key);
		if(object == null){
			return null;
		}else if(object instanceof String){
			return (String)object;
		}
		return null;
	}
	
	public  static String getStr(String key,Record model){
		Object object = model.get(key);
		if(object == null){
			return null;
		}else if(object instanceof String){
			return (String)object;
		}
		return null;
	}
	/**
	 * 获取值，如果为null，返回默认值
	 * @param key
	 * @param model
	 * @param defaultValue
	 * @return
	 */
	public static String getStr(String key,Model<?> model,String defaultValue){
		Object object = model.get(key);
		if(object == null){
			return defaultValue;
		}else if(object instanceof String){
			return (String)object;
		}
		return defaultValue;
	}
	
	public static String getStr(String key, Record model,String defaultValue){
		Object object = model.get(key);
		if(object == null){
			return defaultValue;
		}else if(object instanceof String){
			return (String)object;
		}
		return defaultValue;
	}
}
